package com.bkgy.modular.tbcalsubcoilyieldrate.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.tbcalsubcoilyieldrate.dao.TbCalSubCoilYieldRateMapper;
import com.bkgy.modular.tbcalsubcoilyieldrate.entity.ExportTbCalSubCoilYieldRate;
import com.bkgy.modular.tbcalsubcoilyieldrate.entity.TbCalSubCoilYieldRate;
import com.bkgy.modular.tbcalsubcoilyieldrate.service.TbCalSubCoilYieldRateService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class TbCalSubCoilYieldRateServiceImpl extends ServiceImpl<TbCalSubCoilYieldRateMapper, TbCalSubCoilYieldRate>
    implements TbCalSubCoilYieldRateService {

    @Resource
    private TbCalSubCoilYieldRateMapper tbCalSubCoilYieldRateMapper;

    @Override
    public ResultData findDataTbCalSubCoilYieldRateByPage(JSONObject json) {
        try {
            Integer pageIndex = json.getInteger("pageIndex");
            Integer pageSize = json.getInteger("pageSize");
            //查询条件暂无，后续需要再根据需求添加
            JSONObject condition = json.getJSONObject("condition");
            if(null == pageIndex || null == pageSize){
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }
            List<TbCalSubCoilYieldRate> list = tbCalSubCoilYieldRateMapper.findDataByPage((pageIndex - 1) * pageSize,pageSize * pageIndex,condition);
            Integer count = tbCalSubCoilYieldRateMapper.findCountByPageSize(condition);
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");

        // 创建表格数据集合
        List<TbCalSubCoilYieldRate> listExcel = new ArrayList<>();

        // 调用service查询出数据
        List<TbCalSubCoilYieldRate> data = null;
        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = tbCalSubCoilYieldRateMapper.findDataByPage((i - 1) * pageSize, i * pageSize, condition);
            for (TbCalSubCoilYieldRate item : data) {
                listExcel.add(item);
            }
        }

        List<ExportTbCalSubCoilYieldRate> list = new ArrayList<>();
        for (TbCalSubCoilYieldRate item : listExcel) {
            ExportTbCalSubCoilYieldRate exportTbCalSubCoilYieldRate = new ExportTbCalSubCoilYieldRate();
            BeanUtils.copyProperties(item,exportTbCalSubCoilYieldRate);
            list.add(exportTbCalSubCoilYieldRate);
        }
        try {
            Map<String,Object> data1 = new HashMap();
            data1.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\2130连退成材率.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}




